/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.wwk.core.pullrefresh.core.header;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.RelativeLayout;

import com.wwk.core.R;

public class FlipLoadingLayout extends LoadingLayout {

    static final int FLIP_ANIMATION_DURATION = 150;

    private final Animation mRotateAnimation, mResetRotateAnimation;

    public RelativeLayout refresh_text;

    public FlipLoadingLayout(Context context) {
        super(context);
        final int rotateAngle = -180;
        refresh_text = (RelativeLayout) findViewById(R.id.refresh_text);
        mRotateAnimation = new RotateAnimation(0, rotateAngle, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        //设置动画速率
        mRotateAnimation.setInterpolator(ANIMATION_INTERPOLATOR);
        //设置动画持续时间
        mRotateAnimation.setDuration(FLIP_ANIMATION_DURATION);
        //设置是否停在结束状态
        mRotateAnimation.setFillAfter(true);

        mResetRotateAnimation = new RotateAnimation(rotateAngle, 0, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        mResetRotateAnimation.setInterpolator(ANIMATION_INTERPOLATOR);
        mResetRotateAnimation.setDuration(FLIP_ANIMATION_DURATION);
        mResetRotateAnimation.setFillAfter(true);
        //设置旋转的图片
        setImageDrawable(context.getResources().getDrawable(R.drawable.mls_loading_cap0));
    }

    public void setImageDrawable(Drawable imageDrawable) {
        if (null != imageDrawable) {
            mHeaderImage.setImageDrawable(imageDrawable);
        }
    }

    public void setImageRes(int res) {
        if (0 != res) {
            mHeaderImage.setImageResource(res);
        }
    }

    //下拉时候启动动画
    @Override
    public void onPullToRefresh() {
        super.onPullToRefresh();
        if (mRotateAnimation == mHeaderImage.getAnimation()) {
            mHeaderImage.startAnimation(mResetRotateAnimation);
        }
    }

    //执行刷新 这时候需要把加载动画漏出来
    @Override
    public void onRefresh() {
        super.onRefresh();
        mHeaderImage.clearAnimation();
        mHeaderImage.setVisibility(View.INVISIBLE);
        mHeaderLoading.setVisibility(View.VISIBLE);
    }

    //下拉到刷新点执行动画
    @Override
    public void onRefreshSlopReach() {
        super.onRefreshSlopReach();
        mHeaderImage.startAnimation(mRotateAnimation);
    }

    //下拉到跳到二楼
    @Override
    public void onGoToSecondFloor() {
        super.onGoToSecondFloor();
    }

    @Override
    public void reset() {
        super.reset();
        mHeaderImage.clearAnimation();
        mHeaderLoading.setVisibility(View.GONE);
        mHeaderImage.setVisibility(View.VISIBLE);
    }

}
